/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.llantas.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.llantas.Bl.ArticuloblLocal;
import mx.llantas.modelo.Articulo;

/**
 *
 * @author manuel
 */
@ManagedBean
@SessionScoped
public class ArticleBean {
    @EJB
    private ArticuloblLocal articulobl;
   //------------variables----------------
  private List<Articulo> listaArticulos;
  private Articulo article;
  private String accion;
  private String numeracion;
  private String garantia;
  
  //---------constructor-----
    public ArticleBean() {
        article=new Articulo();
    }

    public String getGarantia() {
        return garantia;
    }

    public void setGarantia(String garantia) {
        this.garantia = garantia;
    }

    public String getNumeracion() {
        return numeracion;
    }

    public void setNumeracion(String numeracion) {
        this.numeracion = numeracion;
    }
    

    public Articulo getArticle() {
        return article;
    }

    public void setArticle(Articulo article) {
        this.article = article;
    }
 

     public List<Articulo> getLista() {
        listaArticulos=articulobl.getAll();
        System.out.println(listaArticulos);        
        return listaArticulos;
    }
    //--------acciones-------
    public String procesarAccion(){
    if(accion.equalsIgnoreCase("nuevo")){
        article.setNumeracion(numeracion);
        
    articulobl.register(article);
    }else{
    if(accion.equalsIgnoreCase("Editar")){
       article.setNumeracion(numeracion);
        
    articulobl.ModiFy(article);
    }else{
    if(accion.equalsIgnoreCase("Eliminar")){
        article.setNumeracion(numeracion);
        
        articulobl.delete(article);
        }
    }
    }
    accion="";
    return "ListaArticulos";

}    
    //-------------listeners----------------
public void prepararNuevo(ActionEvent evt){
    article= new Articulo(); 
    accion="nuevo";
}
public void prepararEditar(ActionEvent evt){
       System.out.println(evt.getComponent().getClientId());
       int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
       System.out.println("el numero es:"+ index);
       article=listaArticulos.get(index);
       accion="Editar";               
}
public void prepararEliminar(ActionEvent ev){
    int index = Integer.parseInt(ev.getComponent().getClientId().split(":")[2]);      
       article=listaArticulos.get(index);
accion="Eliminar";
}

  
   
  

}
